﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Com.Maiyoule.GsmSms.Components {
    /// <summary>
    /// 短信中心号码类型
    /// </summary>
    class AddressType {
        /// <summary>
        /// 未知类型
        /// </summary>
        public const string TYPE_UNKNOWN = "000";
        /// <summary>
        /// 国际类型
        /// </summary>
        public const string TYPE_INTERNATIONAL = "001";
        /// <summary>
        /// 国内类型
        /// </summary>
        public const string TYPE_INTERNAL = "010";
        /// <summary>
        /// 扩展类型
        /// </summary>
        public const string TYPE_EXTEND = "111";

        /// <summary>
        /// 号码鉴别-未知类型
        /// </summary>
        public const string DISTINGUISH_UNKOWN = "0000";
        /// <summary>
        /// 号码鉴别- ISDN/电话号码（E.164/E.163）
        /// </summary>
        public const string DISTINGUISH_ISDN = "0001";
        /// <summary>
        /// 号码鉴别-保留
        /// </summary>
        public const string DISTINGUISH_EXTEND = "1111";



        private string type;
        /// <summary>
        /// 号码类型
        /// </summary>
        public string Type {
            get { return type; }
            set { type = value; }
        }

        private string distinguish;
        /// <summary>
        /// 号码鉴别
        /// </summary>
        public string Distinguish {
            get { return distinguish; }
            set { distinguish = value; }
        }

        /// <summary>
        /// 中心号码类型，默认为 国内-ISDN/电话号码
        /// </summary>
        public AddressType()
            : this(AddressType.TYPE_INTERNATIONAL, AddressType.DISTINGUISH_ISDN) {

        }
        /// <summary>
        /// 中心号码类型
        /// </summary>
        /// <param name="type">号码类型</param>
        /// <param name="distinguish">号码鉴别</param>
        public AddressType(string type, string distinguish) {
            this.type = type;
            this.distinguish = distinguish;
        }

        /// <summary>
        /// 获取号码类型( 编码后的16进制)
        /// </summary>
        /// <returns>返回类型</returns>
        public string GetAddressType() {
            //BIT No.	7	6	5	4	3	2	1	0
            //1	类型	类型	类型	号码鉴别	同3	同3	同3
            //类型：000-未知  001-国际  010-国内  111-留作扩展
            //号码鉴别：0000-未知  0001-ISDN/电话号码（E.164/E.163）  1111-留作扩展
            //return string.Format("{0:x}",Convert.ToInt32(string.Format("1{0}{1}", this.type, this.distinguish),2));

            return Util.BinString2HexString(string.Format("1{0}{1}", this.type, this.distinguish));
        }

        /// <summary>
        /// 设置中心号码类型
        /// </summary>
        /// <param name="type"></param>
        public void SetAddressType(string type) {
            if(string.IsNullOrWhiteSpace(type)){
                return;
            }
            //转为二进制字符串
            string binstr = Util.HexString2BinString(type);
            binstr=binstr.Substring(1);
            this.type = binstr.Substring(0, 3);
            this.distinguish = binstr.Substring(3);
            

        }
    }
}
